package com.ciwong.epaper.modules.epaper.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.ciwong.mobilelib.bean.BaseBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/2.
 */
public abstract class BaseRvAdapter<VH extends RecyclerView.ViewHolder, V extends View, T extends BaseBean> extends RecyclerView.Adapter<VH> {
    private final String TAG = "EpaperFragment";

    protected List<T> mDataList = new ArrayList<>();


    protected int mRowDataCount=1;
    /**
     * 头部不同于列表item 的个数
     *
     * @return
     */
    protected abstract int getTopItemCount();

    public void setDataList(List<T> dataList) {
        if (null != mDataList && dataList != null) {
            mDataList = dataList;
            notifyDataSetChanged();
        }
    }

    public void addDataList(List<T> dataList) {
        if (null != mDataList && dataList != null) {
            mDataList.addAll(dataList);
            notifyDataSetChanged();
        }
    }

    public List<T> getDataList() {
        return  mDataList;
    }

    protected View inflate(ViewGroup parent, int layoutRes) {
        return LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
    }


    @Override
    public int getItemCount() {
        if (mDataList.size() % mRowDataCount == 0) {
            return mDataList.size()/mRowDataCount + getTopItemCount();
        }else {
            return mDataList.size()/mRowDataCount+1 + getTopItemCount();
        }
    }


    protected List<T> getItemDataList(int position) {
        List<T> tList = new ArrayList<T>();

        if (mRowDataCount > 0) {

            for(int i=0;i<mRowDataCount;i++) {

                if ((position * mRowDataCount + i) < mDataList.size())
                {
                    tList.add(mDataList.get(position * mRowDataCount + i));
                }
            }

        }

        return  tList;
    }


    public void setRowDataCount(int rowDataCount) {
        if (rowDataCount > 0) {
            mRowDataCount=rowDataCount;
        }
    }

    public void clear() {
        if(null !=mDataList)
        {
            mDataList.clear();
        }
    }
}
